﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Opc.Ua;
using Opc.Ua.Client;
using OpcUaHelper;

namespace Forging2.Common.Forging
{
    class ForgingRun
    {
        public Services.OPCService.OpcUaClientServer ForgingOPC;
        

        public ForgingRun()
        {
            ForgingOPC = new Services.OPCService.OpcUaClientServer();
        }

        public Heating.HeatingWorkpiece CurrentWorkpiece { get; set; }
        public string BaseHeight { get; set; } = "ns=4;i=23";
        public string CurrentHeight { get; set; } = "ns=4;i=28";
        public string RealHeight { get; set; } = "ns=4;i=26";
        public string FixHeight { get; set; } = "ns=4;i=25";
        public string MinHeight { get; set; } = "ns=4;i=27";
        public string WorkpieceHeight { get; set; } = "ns=4;i=31";
        public string Temperatures1 { get; set; } = "1";
        public string Temperatures2 { get; set; } = "2";
        public string ForgingSignal { get; set; } = "ns=2;i=34";
        public string ZeroSignal { get; set; } = "ns=4;i=36";
        public string SelectSignal { get; set; } = "ns=4;i=35";

        private void FixCurrentHeight()
        {

        }
        private void AnalysisForging()
        {

        }
        private void SelectWorkpiece()
        {

        }
        public void RemoveSubscriptionPLCValue()
        {
            ForgingOPC.OpcUaClient.RemoveAllSubscription();
        }
        public void SubscriptionPLCValue()
        {
            string[] nodeids = new string[] { BaseHeight, CurrentHeight, RealHeight, FixHeight, MinHeight, WorkpieceHeight, ForgingSignal,
                ZeroSignal, SelectSignal,Temperatures1,Temperatures2 };
            if (!ForgingOPC.OpcUaClient.Connected)
            {
                Const.Front.Notification = "信号尚未连接，请重新运行！";
                return;
            }
            
            ForgingOPC.OpcUaClient.AddSubscription(Environment.MachineName, nodeids, PLCValueSubCallback);
        }
      

        public void PLCValueSubCallback(string key, MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args)
        {
            if (key != Environment.MachineName)
            {
                Const.Front.Notification = "机器名不匹配，订阅失败！";
                return;
            }
            MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
            if (DataValue.IsGood(notification.Value))
            {
                //基准值
                if (monitoredItem.StartNodeId.ToString()==BaseHeight)
                {
                    Const.Front.ForgingBaseHeight = notification.Value.WrappedValue.Value.ToString();
                    
                }
                if (monitoredItem.StartNodeId.ToString() == CurrentHeight)
                {
                    Const.Front.ForgingCurrentHeight = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == FixHeight)
                {
                    Const.Front.ForgingFixHeight = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == MinHeight)
                {
                    Const.Front.ForgingMinHeight = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == RealHeight)
                {
                    Const.Front.ForgingRealHeight = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == WorkpieceHeight)
                {
                    Const.Front.ForgingWorkpieceHeight = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == Temperatures1)
                {
                    Const.Front.ForgingTemperatures1 = notification.Value.WrappedValue.Value.ToString();
                }
                if (monitoredItem.StartNodeId.ToString() == Temperatures2)
                {
                    Const.Front.ForgingTemperatures2 = notification.Value.WrappedValue.Value.ToString();
                }
                //选择信号
                if (monitoredItem.StartNodeId.ToString() == SelectSignal)
                {
                    if ((bool)notification.Value.WrappedValue.Value)
                    {
                        SelectWorkpiece();
                    }
                    
                }
                //锻压信号
                if (monitoredItem.StartNodeId.ToString() == ForgingSignal)
                {
                    if ((bool)notification.Value.WrappedValue.Value)
                    {
                        AnalysisForging();
                    }                    
                }
                //归零信号
                if (monitoredItem.StartNodeId.ToString() == ZeroSignal)
                {
                    if ((bool)notification.Value.WrappedValue.Value)
                    {
                        FixCurrentHeight();
                    }
                    
                }
                Console.WriteLine(notification.Value.WrappedValue.Value.ToString());
            }
        }

    }

}